package com.cloud.pole.rpc;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.api.param.PicklistApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.param.model.entity.Picklist;
import com.cloud.pole.param.service.ParamPickListService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/pickList")
public class ParamPickListRest {

	@Autowired
	ParamPickListService paramPickListService;

	@RequestMapping(value = "/getPickList", method = RequestMethod.GET)
	public ApiRestResponse<List<PicklistApi>> getPickList(String picklistCode) {
		try {
			ParamException.err(StringUtils.isBlank(picklistCode), "picklistCode不能为空");
			List<Picklist> srcList = paramPickListService.getPickList(picklistCode);
			List<PicklistApi> descList = new ArrayList<PicklistApi>();
			if (srcList != null) {
				for (Picklist picklist : srcList) {
					PicklistApi picklistVo = new PicklistApi();
					BeanUtils.copyProperties(picklist, picklistVo);
					descList.add(picklistVo);
				}
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, descList);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@RequestMapping(value = "/getPickListText", method = RequestMethod.GET)
	public ApiRestResponse<String> getPickListText(String picklistCode, String picklistValue) {
		try {
			String text = "";
			ParamException.err(StringUtils.isBlank(picklistCode), "picklistCode不能为空");
			ParamException.err(StringUtils.isBlank(picklistValue), "picklistValue不能为空");
			List<Picklist> srcList = paramPickListService.getPickList(picklistCode);
			if (srcList != null) {
				for (Picklist picklist : srcList) {
					if (StringUtils.equals(picklist.getPicklistValue(), picklistValue)) {
						text = picklist.getPicklistText();
						break;
					}
				}
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, text);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@GetMapping("selectPickListBy")
	public ApiRestResponse<Picklist> selectPickListByColdVal(String picklistCode, String picklistValue) {
		try {
			Picklist selectByCodeValue = paramPickListService.selectByCodeValue(picklistCode, picklistValue);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, selectByCodeValue);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	};
}
